package pe.com.gromero.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.web.bind.annotation.RequestParam;

import pe.com.gromero.dao.BrokerDao;
import pe.com.gromero.domain.Broker;


public class BrokerDaoImpl implements BrokerDao {
	@Autowired
	DataSource dataSource;

	protected JdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public DataSource getDataSource() {
		return jdbcTemplate.getDataSource();
	}

	protected JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Broker> getBrokerList() {
		List<Broker> brokerList = new ArrayList<>();
		SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(getJdbcTemplate())
				.withSchemaName("brkesb")
				.withProcedureName("OBTENER_LISTA_BROKERS")
				.returningResultSet("brokers", new BrokerRowMapper());
		Map<String, Object> resultado = simpleJdbcCall.execute();
		brokerList = (List<Broker>) resultado.get("brokers");
		return brokerList;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Broker> obtenerListaEventos() {
		List<Broker> listaEventos = new ArrayList<>();
		SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(getJdbcTemplate())
				.withSchemaName("brkesb")
				.withProcedureName("OBTENER_LISTA_EVENTOS")
				.returningResultSet("eventos", new EventoRowMapper());
		Map<String,Object> resultado=simpleJdbcCall.execute();
		listaEventos=(List<Broker>)resultado.get("eventos");

		return listaEventos;
	}
	

	@Override
	@SuppressWarnings("unchecked")
	public List<Broker> buscarBroker(int cn_correlativo,String cn_msgid) {
		List<Broker> brokerList = new ArrayList<>();
		
		SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(getJdbcTemplate())
				.withSchemaName("brkesb")
				.withProcedureName("BUSQUEDAXFILTRO")
				.returningResultSet("brokers", new BrokerRowMapper());
		
		SqlParameterSource sqlParameterSource=new MapSqlParameterSource()
		.addValue("CN_CORRELATIVO",cn_correlativo )
		.addValue("CN_MSGID" ,cn_msgid);
		
		
		
		Map<String, Object> resultado = simpleJdbcCall.execute(sqlParameterSource);
	
		brokerList = (List<Broker>) resultado.get("brokers");
		return brokerList;
		
		
	}


	private static final class BrokerRowMapper implements RowMapper<Broker> {
		@Override
		public Broker mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			Broker broker = new Broker();
			broker.setCN_CORRELATIVO(resultSet.getInt("CN_CORRELATIVO"));
			broker.setCN_MSGID(resultSet.getString("CN_MSGID"));
			broker.setCN_SOCIEDAD(resultSet.getString("CN_SOCIEDAD"));
			broker.setCN_FLUJO(resultSet.getString("CN_FLUJO"));
			broker.setCN_SECUENCIA(resultSet.getString("CN_SECUENCIA"));
			broker.setCN_CODIGO_APP(resultSet.getString("CN_CODIGO_APP"));
			broker.setCN_EVENTO(resultSet.getString("CN_EVENTO"));
			broker.setCN_TIPO(resultSet.getString("CN_TIPO"));
			broker.setCN_DATETIME(resultSet.getString("CN_DATETIME"));
			broker.setCN_REFERENCIA(resultSet.getString("CN_REFERENCIA"));
			broker.setCN_DATA(resultSet.getString("CN_DATA"));
			broker.setCN_CANAL(resultSet.getString("CN_CANAL"));
			broker.setCN_USR(resultSet.getString("CN_USR"));
			broker.setCN_BROKERNAME(resultSet.getString("CN_BROKERNAME"));
			broker.setCN_CLIENTE(resultSet.getString("CN_CLIENTE"));
			return broker;
		}
	}

	private static final class EventoRowMapper implements RowMapper<Broker> {
		@Override
		public Broker mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			Broker broker = new Broker();
			broker.setCN_EVENTO(resultSet.getString("CN_EVENTO"));
			return broker;
		}

	}


	

}
